package com.example.blogdemo.interceptor;

import com.example.blogdemo.entity.HostHolder;
import com.example.blogdemo.entity.LoginTicket;
import com.example.blogdemo.entity.User;
import com.example.blogdemo.repository.LoginTicketRepository;
import com.example.blogdemo.repository.UserRepository;
import com.example.blogdemo.service.JedisService;
import com.example.blogdemo.util.RedisKeyUntil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
import redis.clients.jedis.Jedis;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;

/**
 * @Author Anita
 * @Date 2017/12/13 14:50
 */
@Slf4j
@Component
public class PassportInterceptor extends HandlerInterceptorAdapter {

    @Autowired
    private LoginTicketRepository loginTicketRepository;

    @Autowired
    private UserRepository userRepository;

    @Autowired
    private HostHolder hostHolder;

    @Autowired
    private JedisService jedisService;

    @Override
    public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o ) throws IOException {
        String ticket = null;
        if(httpServletRequest.getCookies()!=null){
            for(Cookie cookie:httpServletRequest.getCookies()){
                if("ticket".equals(cookie.getName())){
                    ticket = cookie.getValue();
                    break;
                }
            }
        }else{
            httpServletResponse.sendRedirect("/in");
        }
        if(ticket!=null){
            LoginTicket loginTicket = loginTicketRepository.findByTicket(ticket);
            if(loginTicket==null||loginTicket.getExpired().before(new Date())||loginTicket.getStatus()!=0){
                httpServletResponse.sendRedirect("/in");
            }
            User user = userRepository.findById(loginTicket.getUserId());
            hostHolder.setUser(user);
        }
        String uri = httpServletRequest.getServletPath();
        String uriKey = RedisKeyUntil.getClickCountKey(uri);
        String sumKey = RedisKeyUntil.getClickCountKey("SUM");
        jedisService.incr(uriKey);
        jedisService.incr(sumKey);
        return true;
    }

    //可以在这里给每个页面加用户名
    @Override
    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {
        if (modelAndView!=null){
            modelAndView.addObject("user",hostHolder.getUser());
        }
    }

    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {
        hostHolder.clear();
    }





}
